//
//  MessageCenterViewController.m
//  JinMaJiaDemo
//
//  Created by GripMeMory on 16/1/19.
//  Copyright © 2016年 GripMeMory. All rights reserved.
//

#import "MessageCenterViewController.h"
#import "MessageCenterCell.h"
#import "MessageViewController.h"

#define kCellHeight 150 / 2

static NSString * cellIdentifer = @"messageCenterCellName";

@interface MessageCenterViewController () <UITableViewDelegate>

@property (nonatomic, strong) TableViewDataSource *dataSource;

@end

@implementation MessageCenterViewController

{
    NSMutableArray *_titleList;
    NSMutableArray *_modelList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self fakeValue];
    [self customNav];
    [self setupTableView];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.tabBarController.tabBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.tabBarController.tabBar.hidden = NO;
}

#pragma mark - Custom NavigationBar

- (void)fakeValue{
    _titleList = [NSMutableArray arrayWithArray:@[@"站内公告", @"金马甲动态", @"产权机构动态", @"产权新闻", @"投融资动向", @"活动与交流", @"政策解读"]];
    _modelList = [NSMutableArray arrayWithCapacity:10];
    
    for (int i = 0; i < _titleList.count; i++) {
        MessageModel *model = [[MessageModel alloc] init];
        model.title = _titleList[i];
        model.mainMessage = @"标题列表缩略，标题列表缩略标题列表缩略标题列表缩略标题列表缩略，标题列表缩略标题列表缩略，标题列表缩略标题列表缩略标题列表缩略标题列表缩略，标题列表缩略，标题列表缩略标题列表缩略标题列表缩略。";
        model.time = @"2015/11/27  08:13";
        [_modelList addObject:model];
    }
}

- (void)customNav{
    UIBarButtonItem *bbi = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"返回"] style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonItemClick:)];
    self.navigationItem.leftBarButtonItem = bbi;
    
    self.title = @"消息中心";
}

- (void)setupTableView{
    TableViewCellConfigurBlock configureBlock = ^(MessageCenterCell *cell, MessageModel *model){
        [cell configureCellWithModel:model];
    };
    _dataSource = [[TableViewDataSource alloc] initWithItems:_modelList cellIdentifier:cellIdentifer configureBlock:configureBlock];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, UISCREENHEIGHT) style:UITableViewStylePlain];
    [tableView registerClass:[MessageCenterCell class] forCellReuseIdentifier:cellIdentifer];
    tableView.dataSource = _dataSource;
    tableView.delegate = self;
    tableView.backgroundColor = RGB_COLOR(@"#c9c9c9");
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableView];
}

#pragma mark - Actions

- (void)backBarButtonItemClick:(UIBarButtonItem *)bbi{
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)nextMessageViewController:(NSString *)message{
    MessageViewController *mvc = [[MessageViewController alloc] init];
    mvc.title = message;
    [self.navigationController pushViewController:mvc animated:NO];
}

#pragma mark - TableView Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"selected");
    [self nextMessageViewController:_titleList[indexPath.row]];
    NSLog(@"%@", self.navigationController.viewControllers);
}

@end
